package cn.tedu.micro.demo.order.service;

import cn.tedu.micro.demo.common.pojo.po.OrderInfoPO;
import cn.tedu.micro.demo.common.pojo.protocol.JsonResult;
import cn.tedu.micro.demo.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.demo.order.feign.DeviceClient;
import cn.tedu.micro.demo.order.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private DeviceClient deviceClient;
    @Override
    public String createOrder(Integer gunId, Integer userId) {
        log.info("服务客户端deviceClient实现类:{}",deviceClient.getClass().getName());
        //判断枪状态
        JsonResult<GunInfoVO> result = deviceClient.detail(gunId.longValue());
        GunInfoVO gunInfoVO = result.getData();
        if (gunInfoVO!=null){
            log.info("查询结果:{}",gunInfoVO);
            //读取状态 0 离线 1空闲
            Integer status = gunInfoVO.getStatus();
            if (status==01){
                //生成订单
                OrderInfoPO poParam=new OrderInfoPO();
                String billId=UUID.randomUUID().toString();
                poParam.setBillId(billId);
                poParam.setGunId(gunId.longValue());
                poParam.setUserId(userId.longValue());
                orderRepository.save(poParam);
                return billId;
            }
        }
        return null;
    }
}
